package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.IotTimedTasksRpcService;
import com.hunttown.mes.rpc.domain.IotTimedTasksDTO;
import com.hunttown.mes.rpc.domain.query.IotTimedTasksDTOQuery;
import com.hunttown.mes.service.IotTimedTasksService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.IotTimedTasksConverts.convert;

/**
 * created by wangjunfu on 2020-10-05 16:33:38
 */
@Service
public class IotTimedTasksRpcServiceImpl implements IotTimedTasksRpcService {

    private final Logger logger = LoggerFactory.getLogger(IotTimedTasksRpcServiceImpl.class);

    private final IotTimedTasksService iotTimedTasksService;

    @Autowired
    public IotTimedTasksRpcServiceImpl(IotTimedTasksService iotTimedTasksService) {
        this.iotTimedTasksService = iotTimedTasksService;
    }

    @Override
    public IotTimedTasksDTO insert(IotTimedTasksDTO iotTimedTasksDTO) {
        try {
            return convert(iotTimedTasksService.insert(convert(iotTimedTasksDTO)));
        } catch (Exception e) {
            logger.error("IotTimedTasks create failed.", e);
            throw new RuntimeException("IotTimedTasks create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(IotTimedTasksDTO iotTimedTasksDTO) {
        try {
            return iotTimedTasksService.updateInfoById(convert(iotTimedTasksDTO));
        } catch (Exception e) {
            logger.error("IotTimedTasks update by id failed.", e);
            throw new RuntimeException("IotTimedTasks update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(IotTimedTasksDTOQuery iotTimedTasksDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotTimedTasksDTOQuery.getW_id() != null && iotTimedTasksDTOQuery.getW_id() > 0) {
                query.put("w_id", iotTimedTasksDTOQuery.getW_id());
            }

            if (iotTimedTasksDTOQuery.getIds() != null && !iotTimedTasksDTOQuery.getIds().equals("")) {
                query.put("ids", iotTimedTasksDTOQuery.getIds());
            }

            if (iotTimedTasksDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotTimedTasksDTOQuery.getStopFlag());
            }

            if (iotTimedTasksDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotTimedTasksDTOQuery.getDeleteFlag());
            }

            return iotTimedTasksService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("IotTimedTasks update by query failed.", e);
            throw new RuntimeException("IotTimedTasks update by query failed, Please order your code.", e);
        }
    }

    @Override
    public IotTimedTasksDTO selectById(Integer id) {
        try {
            return convert(iotTimedTasksService.selectById(id));
        } catch (Exception e) {
            logger.error("IotTimedTasks select by id failed.", e);
            throw new RuntimeException("IotTimedTasks select by id failed, Please order your code.", e);
        }
    }

    @Override
    public IotTimedTasksDTO selectByIdForWorker(Integer id) {
        return convert(iotTimedTasksService.selectByIdForWorker(id));
    }

    @Override
    public IotTimedTasksDTO selectObjByQuery(IotTimedTasksDTOQuery iotTimedTasksDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(iotTimedTasksService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("IotTimedTasks select obj by query failed.", e);
            throw new RuntimeException("IotTimedTasks select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<IotTimedTasksDTO> getListForPage(IotTimedTasksDTOQuery iotTimedTasksDTOQuery) {
        Query query = Query.page(iotTimedTasksDTOQuery.getPn(), iotTimedTasksDTOQuery.getPageSize());

        if (iotTimedTasksDTOQuery.getId() != null && iotTimedTasksDTOQuery.getId() > 0) {
            query.put("id", iotTimedTasksDTOQuery.getId());
        }

        if (iotTimedTasksDTOQuery.getIds() != null && !iotTimedTasksDTOQuery.getIds().equals("")) {
            query.put("ids", iotTimedTasksDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(iotTimedTasksDTOQuery.getLikeName())) {
            query.put("likeName", iotTimedTasksDTOQuery.getLikeName());
        }

        if (iotTimedTasksDTOQuery.getTaskType() != null && iotTimedTasksDTOQuery.getTaskType() != -9999) {
            query.put("taskType", iotTimedTasksDTOQuery.getTaskType());
        }

        if (iotTimedTasksDTOQuery.getStatus() != null) {
            query.put("status", iotTimedTasksDTOQuery.getStatus());
        }

        if (iotTimedTasksDTOQuery.getIsSystem() != null) {
            query.put("isSystem", iotTimedTasksDTOQuery.getIsSystem());
        }

        if (StringUtils.isNotBlank(iotTimedTasksDTOQuery.getShowDate())) {
            query.put("showDate", iotTimedTasksDTOQuery.getShowDate());
        }

        if (iotTimedTasksDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", iotTimedTasksDTOQuery.getStopFlag());
        }

        if (iotTimedTasksDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", iotTimedTasksDTOQuery.getDeleteFlag());
        }

        if (iotTimedTasksDTOQuery.getOrderSort() != null && iotTimedTasksDTOQuery.getOrderSort() != -9999) {
            if (iotTimedTasksDTOQuery.getOrderSort() == 1) {
                query.put("orderBy", "order by task_type asc, equip_id desc");
            }

            if (iotTimedTasksDTOQuery.getOrderSort() == 2) {
                query.put("orderBy", "order by status asc");
            }

            if (iotTimedTasksDTOQuery.getOrderSort() == 3) {
                query.put("orderBy", "order by equip_id desc");
            }

        } else {
            // 默认排序
            if (StringUtils.isNotBlank(iotTimedTasksDTOQuery.getOrderBy())) {
                query.put("orderBy", iotTimedTasksDTOQuery.getOrderBy());
            }
        }

        List<IotTimedTasksDTO> list = convert(iotTimedTasksService.selectByQuery(query));
        Page<IotTimedTasksDTO> page = Page.from(iotTimedTasksDTOQuery.getPn(), iotTimedTasksDTOQuery.getPageSize(), list);
        page.setTotalItems(iotTimedTasksService.countByQuery(query));

        return page;
    }

    @Override
    public Page<IotTimedTasksDTO> getListForPageForWorker(IotTimedTasksDTOQuery iotTimedTasksDTOQuery) {
        Query query = Query.page(iotTimedTasksDTOQuery.getPn(), iotTimedTasksDTOQuery.getPageSize());

        if (iotTimedTasksDTOQuery.getStatus() != null) {
            query.put("status", iotTimedTasksDTOQuery.getStatus());
        }

        if (iotTimedTasksDTOQuery.getIsSystem() != null) {
            query.put("isSystem", iotTimedTasksDTOQuery.getIsSystem());
        }

        if (iotTimedTasksDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", iotTimedTasksDTOQuery.getStopFlag());
        }

        if (StringUtils.isNotBlank(iotTimedTasksDTOQuery.getOrderBy())) {
            query.put("orderBy", iotTimedTasksDTOQuery.getOrderBy());
        }

        List<IotTimedTasksDTO> list = convert(iotTimedTasksService.selectByQueryForWorker(query));
        Page<IotTimedTasksDTO> page = Page.from(iotTimedTasksDTOQuery.getPn(), iotTimedTasksDTOQuery.getPageSize(), list);
        page.setTotalItems(0);

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return iotTimedTasksService.deleteById(id);
        } catch (Exception e) {
            logger.error("IotTimedTasks delete failed.", e);
            throw new RuntimeException("IotTimedTasks delete failed, Please order your code.", e);
        }
    }
}